package com.arvato.wxwork.controller.front.member;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.wxwork.bean.bean_do.member.WxworkTagDo;
import com.arvato.wxwork.bean.bean_do.member.WxworkUserTagDo;
import com.arvato.wxwork.bean.bean_vo.member.WxworkTagVo;
import com.arvato.wxwork.bean.bean_vo.member.WxworkUserTagVo;
import com.arvato.wxwork.service.member.WxworkTagService;

/**
 * 前台标签相关接口
 * 
 * @author ding
 */
@RestController
@RequestMapping("/wxwork/tag")
public class WxworkTagController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private WxworkTagService wxworkTagService;

	/**
	 * 获取标签列表
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list")
	public Result listTag(@RequestParam Map<String, Object> param) {
		logger.info("调用标签列表接口(tag)");
		Query query = new Query(param);
		List<WxworkTagVo> list = wxworkTagService.listTagByQuery(query);
		return Result.success(list);
	}

	/**
	 * 查询一个会员的所有标签 查询一个员工创建的所有标签
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list/user")
	public Result listUserTag(@RequestParam Map<String, Object> param) {
		logger.info("调用标签列表接口（user-tag）");
		Query query = new Query(param);
		List<WxworkUserTagVo> list = wxworkTagService.listUserTagByQuery(query);
		return Result.success(list);
	}

	/**
	 * 给用户绑定标签 需要参数 staffId tagName externalId
	 * 
	 * @param tagDo
	 * @param userTagDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/bind")
	public Result bindTag(String staffId, String externalId, String tagNameStr, String brand) {
		logger.info("调用标签和用户绑定接口");
		List<String> failList = new ArrayList<String>();
		String[] tagNameArray = tagNameStr.split(",");
		for(String tagName : tagNameArray) {
			WxworkTagDo tagDo = new WxworkTagDo();
			tagDo.setTagName(tagName);
			tagDo.setType("2");
			tagDo.setBrand(brand);
			WxworkUserTagDo userTagDo = new WxworkUserTagDo();
			userTagDo.setExternalId(externalId);
			userTagDo.setStaffId(staffId);
			try {
				int row = wxworkTagService.bindTag(tagDo, userTagDo);
				if (row == 0) {
					failList.add(tagName);
				}
			} catch (DuplicateKeyException e) {
				failList.add(tagName);
				logger.error("绑定标签失败:" + e.getMessage());
			}
		}
		if(failList.size() == 0) {
			return Result.success("添加成功。");
		}
		return Result.fail("添加失败", failList);
	}

	/**
	 * 解除用户和标签的绑定
	 * 
	 * @param bindId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/unbind")
	public Result unbindTag(String bindId) {
		logger.info("调用解除用户和标签的绑定接口");
		int row = wxworkTagService.unbindTag(bindId);
		if (row == 1) {
			return Result.success(row);
		}
		return Result.fail("操作失败");
	}

}
